<template>
    <div class="mainBox">
        <div class="head">New Game</div>
        <div class="body">
            <div class="title">Select a number</div>
            <div class="content">
                <input v-on:blur="inputOnBlur" required="required" ref="inputVal" v-bind:class="{inputFocus:checkInputEmpty}" placeholder="Between 10 to 20" v-model="num" type="number" min="10" max="20">
                <div class="notice" v-bind:class="{showEmptyTips:!checkInputEmpty}">
                        <div class="triangle"/>
                        <div class="innercontent">
                            <div class="logo">
                                <img src="../assets/image/notice.svg" alt=""/>
                                <div class="text">请填写此字段</div>
                            </div>
                        </div>
                </div>
                <div class="selectors">
                    <select v-model="selectCategory">
                        <option  v-for="item in categories" v-bind:value="item.id">{{item.name}}</option>
                    </select>
                    <select v-model="selectDifficult">
                        <option  v-for="item in difficulties" v-bind:value="item.id">{{item.name}}</option>
                    </select>
                    <select v-model="selectType">
                        <option  v-for="item in types" v-bind:value="item.id">{{item.name}}</option>
                    </select>
                </div>
                <div class="submitDiv">
                    <button v-on:click="submit">提交</button>
                </div>
            </div>
        </div>
        <router-view></router-view>
    </div>
</template>

<script>
    import {request} from "../network/request";
    export default {
        name: "home",
        data(){
            return{
                categories:[{'name':'Category','id':''}],
                difficulties:[{'name':'Difficulty','id':''},
                    {'name':'Easy','id':'easy'},
                    {'name':'Medium','id':'medium'},
                    {'name':'Hard','id':'hard'},],
                types:[{'name':'Type','id':''},
                    {'name':'Multiple Choice','id':'multiple'},
                    {'name':'True or False','id':'boolean'}],
                num:'',
                selectDifficult:'',
                selectCategory:'',
                selectType:'',
                checkInputEmpty:''
            }
        },
        methods:{
            submit:function(){
                let _this=this;
                console.log(_this.selectCategory);
                if(!this.checkParam()){
                    return;
                }
                //请求数据
                let config={
                    url:"https://opentdb.com/api.php",
                    params:{
                        "amount":_this.num,
                        "category":_this.selectCategory,
                        "difficulty":this.selectDifficult,
                        "type":this.selectType
                    }
                };
                request(config).then((ret)=>{
                    //在PUSH行为中，path要和query一起使用，name和params一起使用
                    /*this.$router.push({
                        path:'/answer',
                        query:{
                            ret:'ret'
                        }
                    });*/
                    _this.$router.push({
                        name:'Answer',
                        params:{
                            ret:'ret'
                        }
                    })
                });

            },
            checkParam:function () {
                //!!的意思是判断一个变量是否不为空和不为null
                if(this.num===''){
                    this.checkInputEmpty=true;
                    this.$nextTick(()=>{
                        this.$refs.inputVal.focus();
                    });
                    return false;
                }
  /*              if(this.selectDifficult && this.selectDifficult!=='0'){

                }
                if(!!this.selectCategory && this.selectCategory!=='0'){
                    console.log("222")
                }
                if(!!this.selectType && this.selectType!=='0'){

                }*/
                return true;
            },
            inputOnBlur:function () {
                this.checkInputEmpty=false;
                console.log('input on blur')
            }
        },
        created() {
            let array=[];
            let config={
                url:"https://opentdb.com/api_category.php",
                timeout:10000
            };
            request(config).then(ret=>{
                array=ret.data.trivia_categories;
                array.forEach(data=>{
                    let obj={'name':data.name,'id':data.id};
                    this.categories.push(obj)
                });
            });
        },
        watch:{
            selectDifficult:function (newVal, oldVal) {
                console.log(newVal);
            },
            selectCategory:function (newVal, oldVal) {
                console.log(newVal);
            },
            selectType:function (newVal, oldVal) {
                console.log(newVal);
            }
        }
       /* watch:{
            num:function (newVal,oldVal) {
                console.log('oldVal:'+oldVal);
                console.log('newVal:'+newVal);
            }
        }*/
    }
</script>

<style scoped>
    @import "../assets/css/notice.css";
    /*输入框被点击时*/
    .body .content input:focus{
        /*外边框*/
        outline: 0;
        /*border: solid 1px red;*/
        /*box-shadow:0 1px 5px rgba(0,123,255,.25);*/
        box-shadow:0 0 0 0.2rem rgba(0,123,255,.25);
    }
    .body .content select:focus{
        /*外边框*/
        outline: 0;
        /*border: solid 1px red;*/
        box-shadow:0 0 0 0.2rem rgba(0,123,255,.25);
    }
    .body .content button:focus{
        box-shadow:0 0 0 0.2rem rgba(0,123,255,.25);
    }
    .body .content button:hover{
        background-color: #59aa73!important;
    }

    *{
        font-size: 16px;
        font-weight: 400;
        line-height: 1.5;
        box-sizing: border-box;
        background-color: #f8f9fa;
    }
    .showEmptyTips{
        display: none;
    }
    .inputFocus{
        outline: 0;
        box-shadow:0 0 0 0.2rem rgba(0,123,255,.25);
    }
    .mainBox{
        width: 463px;
        height: 400px;
        margin: 0 auto;
        border: solid 1px rgba(0,0,0,.125);
        background-color: #fff;
        border-radius: .25rem;
    }
    .head{
        height: 50px;
        border:solid 1px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        background-color: #343a40!important;
        color: white;
        font-weight: 700!important;
    }
    .body{
        padding: 1.25rem;
        background-color: #fff;
    }
    .body .title{
        /*border: solid 1px blue;*/
        margin-bottom: 5px;
        background-color: #fff;
    }
    .body .content input{
        border: solid 1px #ced4da;
        border-radius: .25rem;
        padding: .375rem .75rem;
        width: 100%;
    }

    .selectors select{
        margin-top: 1rem;
        width: 100%;
        border: solid 1px #ced4da;
        border-radius: .25rem;
        padding: .375rem .75rem;
    }
    .body .content button{
        margin-top: 1rem;
        background-color: #59dd99!important;
        border-color: #59aa73!important;
        padding: .275rem .95rem;
        border-radius: .25rem;
    }
</style>